package com.cy.service;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cy.bfzcommons.mapper.OrdersMapper;
import com.cy.bfzcommons.mapper.ProductMapper;
import com.cy.bfzcommons.mapper.UserMapper;
import com.cy.bfzcommons.pojo.Orders;
import com.cy.bfzcommons.pojo.Product;
import com.cy.bfzcommons.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class OrdersServiceImpl implements OrdersService {

    // 订单 模块
    @Autowired
    private OrdersMapper ordersMapper;

    //商品
    @Autowired
    private ProductMapper productMapper;

    //用户
    @Autowired
    private UserMapper userMapper;

    // 订单删除
    @Override
    @Transactional
    public void deleteOrders(Integer ordersId) {
        ordersMapper.deleteById(ordersId);
    }


    // 查询所有订单
    @Override
    public List<Orders> getOrders() {
        return ordersMapper.selectList(null);
    }

    // 修改订单
    @Override
    @Transactional
    public void upOrders(Orders orders) {
        ordersMapper.updateById(orders);
    }

    // 订单新增
    @Override
    public Integer addOrders(Orders orders){

        orders.setOrderNumber(Integer.valueOf(UUID.randomUUID().toString().hashCode()));
        orders.setWaybillNumber(String.valueOf(UUID.randomUUID().toString().hashCode()));
        ordersMapper.insert(orders);
        System.out.println(orders.getOrdersId());
        return  orders.getOrdersId();
    }

    //下面是后台使用的请求-------------------------------------
    @Override
    public List<Orders> getOrders2(Integer orderNumber) {

        return ordersMapper.selectOrderslist(orderNumber);
    }


    //下面是前端页面使用的请求_________________________________________________________________________________________

    @Override
    public Orders selectOrderById(Integer ordersId) {
        Orders orders = ordersMapper.selectById(ordersId);
        orders.setProduct(productMapper.selectById(orders.getProductId()));
        orders.setUser(userMapper.selectById(orders.getUserId()));

        return orders;
    }

    @Override
    public List<Orders> userOrderByIdState(Integer userId,Integer state) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("status", state);
        List<Orders> orders = ordersMapper.selectList(queryWrapper);
        for (Orders order : orders){
            Product product = productMapper.selectById(order.getProductId());
            order.setProduct(product);
        }
        return orders;
    }

    @Override
    public List<Orders> userOrderById(Integer userId) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<Orders> orders = ordersMapper.selectList(queryWrapper);
        for (Orders order : orders){
            Product product = productMapper.selectById(order.getProductId());
            order.setProduct(product);
        }
        return orders;
    }

    @Override
    public Integer updateStatus(Integer ordersId, Integer status) {
        Orders o = new Orders();
        o.setOrdersId(ordersId).setStatus(status);
        int i = ordersMapper.updateById(o);
        return i;
    }
}
